package uk.ac.qub.midorfeed;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Utility class communicating with the hero suggestion server
 * @author Graeme
 */
public final class HttpUtils {
	
	/**
	 * Constructor set to private as all methods are static
	 */
	private HttpUtils() {	
	}

	/**
	 * Determines whether or not the device has a network connection.
	 * 
	 * @param context - context of the calling activity.
	 * @return true if network access is available, false otherwise.
	 */
	public static boolean isConnected(final Context context) {
		
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		
		if (networkInfo != null && networkInfo.isConnected()){
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Executes an HTTP GET using parameters specified and returns the result in a string format.
	 * @param requestURL URL to use
	 * @param requestParameters Parameters to append to the URL.
	 * @return the response from the server, if available.
	 */
	public static String getResponse(String requestURL,String requestParameters) {
		
		InputStream inputStream = null;
		String result = "";
		
		try {
			//creates a URL for use by the server web service
			final String URL = requestURL + requestParameters;
			// instantiates a Http client for use with the get request
			final HttpClient httpclient = new DefaultHttpClient();
			//executes a http get request using URL String
			final HttpResponse httpResponse = httpclient.execute(new HttpGet(URL));
			//input stream containing suggested heroes from server
			inputStream = httpResponse.getEntity().getContent();
			//calls method to convert the input stream to a string
			if (inputStream != null) {
				result = convertInputStreamToString(inputStream);
			} else {
				result = "Unable to retrieve response.";
			}
		} catch (Exception e) {
			Log.e( "HS", "Error retrieving response", e );
		}
		return result;
	}

	/**
	 * method to convert an input stream to a string.
	 * 
	 * @param inputStream stream to use.
	 * @return string representation of input stream contents.
	 * 
	 * @throws IOException an error occurred parsing input stream.
	 */
	public static String convertInputStreamToString(InputStream inputStream) throws IOException {
		
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
		
		String line = "";
		String result = "";
		
		while ((line = bufferedReader.readLine()) != null)
			result += line;

		inputStream.close();
		return result;
	}
}
